class FrameGui extends Gui {
  ; An intermediate

  __New() {
    super.__New('+AlwaysOnTop -Caption +ToolWindow +E0x00080000', Meta.name 'ClipGUI')
    this.borderPen := Gdip_CreatePen('0xffc0efa1', 1) ; 边框大小和颜色
    this.staticHdc := GetDC(StaticBG.insId)
    this.Show()
  }

  Destroy() {
    Gdip_DeletePen(this.borderPen)
    ReleaseDC(this.staticHdc)
    super.Destroy()
  }

  GetPos(&x, &y, &w, &h) => DCon(this.pos, &x, &y, &w, &h)

  Clamp() { ; 确保不会超出屏幕
    this.GetPos(&x, &y, &w, &h)
    wx := w < 0 ? x + w : x, wy := h < 0 ? y + h : y, ww := Abs(w), wh := Abs(h)
    if wx < 0
      ww += wx, wx := 0
    if wy < 0
      wh += wy, wy := 0
    if wx + ww > A_ScreenWidth
      ww := A_ScreenWidth - wx
    if wy + wh > A_ScreenHeight
      wh := A_ScreenHeight - wy
    this.Move(wx, wy, ww, wh)
  }

  Move(x?, y?, w?, h?) {
    DCon(this.pos, &_x, &_y, &_w, &_h)
    IsSet(x) && _x := x
    IsSet(y) && _y := y
    IsSet(w) && _w := w
    IsSet(h) && _h := h
    this.DrawFrame(_x, _y, _w, _h)
  }

  DrawFrame(x, y, w, h) {
    this.pos := [x, y, w, h]
    x := w < 0 ? x + w : x, y := h < 0 ? y + h : y
    w := Abs(w), h := Abs(h)

    hdc := CreateCompatibleDC()
    hbm := CreateDIBSection(w, h)
    obm := SelectObject(hdc, hbm)
    G := Gdip_GraphicsFromHDC(hdc)

    BitBlt(hdc, 0, 0, w, h, this.staticHdc, x, y)
    Gdip_DrawRectangle(G, this.borderPen, 1, 1, w - 2, h - 2)
    PixelGetColor(0, 0) ; FIXME
    UpdateLayeredWindow(this.Hwnd, hdc, x, y, w, h)

    SelectObject(hdc, obm)
    DeleteObject(hbm)
    DeleteDC(hdc)
    Gdip_DeleteGraphics(G)
  }

  ConvertToPasteGui() {
    DCon(this.pos, &x, &y, &w, &h)
    g := PasteGui(), g.Show(x, y, w, h)
    return g
  }
}